package com.hhkj.study.admin.order.service.extend.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hhkj.study.admin.order.entity.Order;
import com.hhkj.study.admin.order.model.podo.OrderDo;
import com.hhkj.study.admin.order.model.podo.OrderPo;
import com.hhkj.study.admin.order.model.res.OrderRes;
import com.hhkj.study.admin.order.service.base.OrderService;
import com.hhkj.study.admin.order.service.extend.OrderExtendService;
import com.ruoyi.common.base.biz.service.extend.BaseExtendServiceImpl;
import com.ruoyi.common.cont.SysConst;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
*
* @author SERVER
* @since 2024-09-18
*/
@Service
public class OrderExtendServiceImpl extends BaseExtendServiceImpl<Order, OrderDo, OrderPo, OrderService> implements OrderExtendService {
    @Override
    public Map<Long, Pair<Integer, Integer>> statUserOrder(List<Long> userIdList) {

        LambdaQueryWrapper<Order> orderQuery = new LambdaQueryWrapper<>();
        orderQuery.in(Order::getPayUserId, userIdList);
        List<Order> orderList = this.getService().list(orderQuery);
        if (CollectionUtil.isEmpty(orderList)) {
            return Maps.newHashMap();
        }
        return orderList.stream().collect(Collectors.groupingBy(Order::getPayUserId
                , Collectors.collectingAndThen(Collectors.toList()
                        , list -> Pair.of(list.size()
                                , list.stream().map(Order::getPayPrice).reduce(0, Integer::sum)))));
    }

    @Override
    public List<OrderRes> listByUserId(Long userId) {
        LambdaQueryWrapper<Order> orderQuery = new LambdaQueryWrapper<>();
        orderQuery.eq(Order::getPayUserId, userId);
        List<Order> orderList = this.getService().list(orderQuery);
        if (CollectionUtil.isEmpty(orderList)) {
            return Lists.newArrayList();
        }
        return BeanUtil.copyToList(orderList, OrderRes.class);
    }
}
